//
// Created by vista on 2017/12/9.
//

#ifndef P_P6_H
#define P_P6_H

#include <vector>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;

class Solution {
public:
    string convert(string s, int numRows) {
        if(s.length() == 0)
            return s;

        vector< vector<char> > rows (static_cast<unsigned long>(numRows));

        auto size = s.length();

        int inext = 2 * numRows - 2;
        if( numRows == 1)
            inext = 1;

        for (int k = 0; k < numRows; ++k) {
            for (int i = k; i < size; i+= inext ) {
                rows[k].push_back( s[i] );

                if(k != 0 && k != numRows - 1 ){
                    auto j = static_cast<int>((rows[k].size() / 2 + 1) * inext - k);
                    if(j < size)
                        rows[k].push_back(s[j]);
                }
            }
        }

        string ss(size,'s');
        int index = 0;
        for (int j = 0; j < numRows; ++j) {
            auto itr = rows[j].begin(),eitr = rows[j].end();
            for (; itr != eitr ; ++itr) {
                ss[index++] = *itr;
            }
        }
        return ss;
    }
};
#endif //P_P6_H
